home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / source.exe / POSIX / SH / STD / STDC / STRTOK.C < prev    next >
C/C++ Source or Header  |  1992-07-13  |  1KB  |  63 lines

  1. #include <string.h>
  2.  
  3. /*
  4.  * Get next token from string s (NULL on 2nd, 3rd, etc. calls),
  5.  * where tokens are nonempty strings separated by runs of
  6.  * chars from delim.  Writes NULs into s to end tokens.  delim need not
  7.  * remain constant from call to call.
  8.  */
  9.  
  10. static char *scanpoint = NULL;
  11.  
  12. char *                /* NULL if no token left */
  13. strtok(s, delim)
  14. char *s;
  15. register Const char *delim;
  16. {
  17.     register char *scan;
  18.     char *tok;
  19.     register Const char *dscan;
  20.  
  21.     if (s == NULL && scanpoint == NULL)
  22.         return(NULL);
  23.     if (s != NULL)
  24.         scan = s;
  25.     else
  26.         scan = scanpoint;
  27.  
  28.     /*
  29.      * Scan leading delimiters.
  30.      */
  31.     for (; *scan != '\0'; scan++) {
  32.         for (dscan = delim; *dscan != '\0'; dscan++)
  33.             if (*scan == *dscan)
  34.                 break;
  35.         if (*dscan == '\0')
  36.             break;
  37.     }
  38.     if (*scan == '\0') {
  39.         scanpoint = NULL;
  40.         return(NULL);
  41.     }
  42.  
  43.     tok = scan;
  44.  
  45.     /*
  46.      * Scan token.
  47.      */
  48.     for (; *scan != '\0'; scan++) {
  49.         for (dscan = delim; *dscan != '\0';)    /* ++ moved down. */
  50.             if (*scan == *dscan++) {
  51.                 scanpoint = scan+1;
  52.                 *scan = '\0';
  53.                 return(tok);
  54.             }
  55.     }
  56.  
  57.     /*
  58.      * Reached end of string.
  59.      */
  60.     scanpoint = NULL;
  61.     return(tok);
  62. }
  63.